package com.micro.service.controller;

import com.micro.service.dao.AccountRepository;
import com.micro.service.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Create by Vic Xu on 2019/3/25
 */
@RestController
@RequestMapping(value = "/v1/account/user", produces = {"application/json;charset=UTF-8"})
public class AccountUserController {

//    @Autowired
//    private AccountRepository accountRepository;

    /**
     * 初始化用户数据
     */
//    @Autowired
//    public void init() {
//
//        // 为了方便测试,这里添加了两个不同角色的账户
//        accountRepository.deleteAll();
//        PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
//        Account accountA = new Account();
//        accountA.setUserName("admin");
//        accountA.setPassword(passwordEncoder.encode("admin"));
//        accountA.setRoles(new String[]{"ROLE_ADMIN", "ROLE_USER"});
//        accountRepository.save(accountA);
//
//        Account accountB = new Account();
//        accountB.setUserName("guest");
//        accountB.setPassword(passwordEncoder.encode("guest"));
//        accountB.setRoles(new String[]{"ROLE_GUEST"});
//        accountRepository.save(accountB);
//    }


    @GetMapping()
    public Principal user(Principal user) {
        return user;
    }

}
